package com.itheima.Test;

import com.itheima.domain.Orders;
import com.itheima.domain.User;

import com.itheima.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

public class Demo1 {
    public static void main(String[] args) throws Exception{
        /**
         * 加载资源文件
         * 创建sqlSessionfactory对象
         * 创建session对象
         */
        InputStream is = Resources.getResourceAsStream("sql.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
        SqlSession session = build.openSession();
        /**
         * 获得ordersMapper的实现类对象
         * 调用对象的findAll方法
         * 遍历循环
         */
        UserMapper mapper = session.getMapper(UserMapper.class);
        List<User> list = mapper.findAll();
        for (User user : list) {
            System.out.println("用户名:"+user.getUsername());
            List<Orders> orders = user.getList();
            for (Orders order : orders) {
                System.out.println("订单编号:"+order.getNumber());
            }
        }
        /**
         * 提交事务
         * 释放资源
         */
        session.commit();
        session.close();
    }
}
